﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BlinkyDevice
{
    public partial class BlinkyControl : UserControl
    {
        private bool redOn = true;
        private bool greenOn = true;

        public BlinkyControl()
        {
            InitializeComponent();
        }

        public bool GreenOn
        {
            get
            {
                return greenOn;
            }
            set
            {
                if (greenOn != value)
                {
                    UpdateGreenButton();
                }
            }
        }

        public bool RedOn
        {
            get
            {
                return redOn;
            }
            set
            {
                if (redOn != value)
                {
                    UpdateRedButton();
                }
            }
        }

        private void UpdateRedButton()
        {
            if (Enabled)
            {
                redButton.Enabled = true;
                if (redOn)
                {
                    redOn = false;
                    redButton.Text = "Off";
                }
                else
                {
                    redOn = true;
                    redButton.Text = "On";
                }
            }
            else
            {
                redButton.Enabled = false;
                redOn = true;
                redButton.Text = "On";
            }
        }

        private void UpdateGreenButton()
        {
            if (Enabled)
            {
                greenButton.Enabled = true;
                if (greenOn)
                {
                    greenOn = false;
                    greenButton.Text = "Off";
                }
                else
                {
                    greenOn = true;
                    greenButton.Text = "On";
                }
            }
            else
            {
                greenButton.Enabled = false;
                greenOn = true;
                greenButton.Text = "On";
            }
        }

        public event RedOnChangedEventHandler OnRedOnChanged;
        public event GreenOnChangedEventHandler OnGreenOnChanged;

        protected override void OnEnabledChanged(EventArgs e)
        {
            UpdateRedButton();
            UpdateGreenButton();
            base.OnEnabledChanged(e);
        }

        private void redButton_Click(object sender, EventArgs e)
        {
            if (OnRedOnChanged != null)
            {
                UpdateRedButton();
                OnRedOnChanged(this, new RedOnChangedArgs(RedOn));
            }
        }

        private void greenButton_Click(object sender, EventArgs e)
        {
            if (OnGreenOnChanged != null)
            {
                UpdateGreenButton();
                OnGreenOnChanged(this, new GreenOnChangedArgs(GreenOn));
            }
        }
    }

    public class RedOnChangedArgs : EventArgs
    {
        public readonly bool RedOn;

        public RedOnChangedArgs(bool b)
        {
            RedOn = b;
        }
    }
    public class GreenOnChangedArgs : EventArgs
    {
        public readonly bool GreenOn;

        public GreenOnChangedArgs(bool b)
        {
            GreenOn = b;
        }
    }

    public delegate void RedOnChangedEventHandler(object sender, RedOnChangedArgs args);
    public delegate void GreenOnChangedEventHandler(object sender, GreenOnChangedArgs args);
}
